package com.mujin.keji.collectionsystem.module.mypager.views;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.mujin.keji.collectionsystem.CcsApplication;
import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.base.BaseDto;
import com.mujin.keji.collectionsystem.module.mypager.dto.UpdatePwdDto;
import com.mujin.keji.collectionsystem.module.mypager.viewmodel.UpdatePwdViewModel;
import com.mujin.keji.collectionsystem.utils.ToastUtils;

import java.util.regex.Pattern;

/**
 * 修改密码界面
 *
 * @author weishixiong
 * @Time 2018-03-20
 */

public class UpdatePwdActivity extends BaseActivity implements View.OnClickListener {
    private EditText evOldPwd;//输入旧密码
    private EditText etNewPwd;//输入新密码
    private EditText etConfirmPwd;//再次输入新密码
    private UpdatePwdViewModel updatePwdViewModel;
    private Button btnOk;
    private RelativeLayout rlOldPwd;
    private ImageView ivOldEyes;
    private RelativeLayout rlNewPwd;
    private ImageView ivNewEyes;
    private RelativeLayout rlConfirmPwd;
    private ImageView ivConfirmEyes;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CcsApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CcsApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        updatePwdViewModel = ViewModelProviders.of(this).get(UpdatePwdViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_updatepwd;
    }

    @Override
    public void initView() {
        setCenterTitle("修改登录密码", "#FFFFFF");
        setleftImage(R.drawable.image_arrow_left_white, true, null);
        evOldPwd = (EditText) findViewById(R.id.ev_old_pwd);
        etNewPwd = (EditText) findViewById(R.id.et_new_pwd);
        etConfirmPwd = (EditText) findViewById(R.id.et_confirm_pwd);
        btnOk = (Button) findViewById(R.id.btn_ok);
        rlOldPwd = (RelativeLayout) findViewById(R.id.rl_old_pwd);
        ivOldEyes = (ImageView) findViewById(R.id.iv_old_eyes);
        rlNewPwd = (RelativeLayout) findViewById(R.id.rl_new_pwd);
        ivNewEyes = (ImageView) findViewById(R.id.iv_new_eyes);
        rlConfirmPwd = (RelativeLayout) findViewById(R.id.rl_confirm_pwd);
        ivConfirmEyes = (ImageView) findViewById(R.id.iv_confirm_eyes);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        btnOk.setOnClickListener(this);
        rlOldPwd.setOnClickListener(this);
        rlNewPwd.setOnClickListener(this);
        rlConfirmPwd.setOnClickListener(this);
        etConfirmPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(evOldPwd.getText().toString()) ||
                        TextUtils.isEmpty(etNewPwd.getText().toString()) ||
                        TextUtils.isEmpty(etConfirmPwd.getText().toString())) {
                    btnOk.setEnabled(false);
                    btnOk.setTextColor(Color.parseColor("#aaFFFFFF"));
                } else {
                    btnOk.setEnabled(true);
                    btnOk.setTextColor(Color.parseColor("#FFFFFF"));
                }
            }
        });
        etNewPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(evOldPwd.getText().toString()) ||
                        TextUtils.isEmpty(etNewPwd.getText().toString()) ||
                        TextUtils.isEmpty(etConfirmPwd.getText().toString())) {
                    btnOk.setEnabled(false);
                    btnOk.setTextColor(Color.parseColor("#aaFFFFFF"));
                } else {
                    btnOk.setEnabled(true);
                    btnOk.setTextColor(Color.parseColor("#FFFFFF"));
                }
            }
        });
        evOldPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(evOldPwd.getText().toString()) ||
                        TextUtils.isEmpty(etNewPwd.getText().toString()) ||
                        TextUtils.isEmpty(etConfirmPwd.getText().toString())) {
                    btnOk.setEnabled(false);
                    btnOk.setTextColor(Color.parseColor("#aaFFFFFF"));
                } else {
                    btnOk.setEnabled(true);
                    btnOk.setTextColor(Color.parseColor("#FFFFFF"));
                }
            }
        });
    }
    private boolean oldYes;
    private boolean newYes;
    private boolean confirmYes;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                updatePwd();
                break;
            case R.id.rl_old_pwd:
                if(!oldYes){
                    evOldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivOldEyes.setImageResource(R.drawable.image_login_open);
                }
                else{
                    evOldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivOldEyes.setImageResource(R.drawable.image_login_close);
                }
                oldYes = !oldYes;
                break;
            case R.id.rl_new_pwd:
                if(!newYes){
                    etNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivNewEyes.setImageResource(R.drawable.image_login_open);
                }
                else{
                    etNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivNewEyes.setImageResource(R.drawable.image_login_close);
                }
                newYes = !newYes;
                break;
            case R.id.rl_confirm_pwd:
                if(!confirmYes){
                    etConfirmPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivConfirmEyes.setImageResource(R.drawable.image_login_open);
                }
                else{
                    etConfirmPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivConfirmEyes.setImageResource(R.drawable.image_login_close);
                }
                confirmYes = !confirmYes;
                break;
        }
    }

    /**
     * 修改密码
     */
    private void updatePwd() {
        String retxtpwd = "^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{8,20}$";
        if (!Pattern.matches(retxtpwd, etNewPwd.getText().toString())) {
            ToastUtils.showToast("新密码为8-20位,大写字母，数字和小写字母至少包含各一个");
            return;
        }
        if (!Pattern.matches(retxtpwd, etConfirmPwd.getText().toString())) {
            ToastUtils.showToast("新密码为8-20位,大写字母，数字和小写字母至少包含各一个");
            return;
        }
        if (!etConfirmPwd.getText().toString().equals(etNewPwd.getText().toString())) {
            ToastUtils.showToast("两次输入密码不一致");
            return;
        }
        getLoadingDialog().showDialog();
        updatePwdViewModel.getLiveData(etNewPwd.getText().toString(), evOldPwd.getText().toString()).observe(this, new Observer<BaseDto<UpdatePwdDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<UpdatePwdDto> updatePwdDtoBaseDto) {
                getLoadingDialog().dismiss();
                if (updatePwdDtoBaseDto.getCode() == Constant.Server.SUCCESS_CODE) {
                    finish();
                    CcsApplication.logout(Constant.Server.I_LOGOUT);
                }
                ToastUtils.showToast(updatePwdDtoBaseDto.getMsg());

            }
        });
    }
}
